package pattern.observer.inbuilt

import java.util.{Observable, Observer}

/**
 * Created by root on 15-9-17.
 */
class CurrentConditionsDisplay(observable: Observable) extends DisplayElement with Observer{
  private var temperature:Float = _
  private var humidity:Float = _

  override def display(): Unit = {
    println("Current conditions:"+temperature+"F degrees and "+humidity+"% humidity")
  }

  //这个方法是Oberver自带的，就是给Observable改变的时候调用的
  override def update(o: Observable, arg: scala.Any): Unit = {
    if(o.isInstanceOf[WeatherData]){
      val weatherDate = o.asInstanceOf[WeatherData]
      this.temperature = weatherDate.getTemperature()
      this.humidity = weatherDate.getHumidity()
      display()
    }
  }

  observable.addObserver(this)
}
